package com.feituan.wx.controller;


import com.feituan.core.persistence.service.IIntegrationProductService;
import com.feituan.core.pojo.entity.IntegrationProduct;
import com.feituan.core.pojo.request.IntegrationProductReq;
import com.feituan.core.pojo.response.IntegrationProductRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 后台管理
 */
@RestController
@RequestMapping("/integrationProduct")
public class IntegrationProductController {

    @Resource
    private IIntegrationProductService integrationProductService;

    /**
     * 新增产品
     * @param product product
     * @return
     */
    @PostMapping("/save")
    public Response<Boolean> saveProduct(IntegrationProduct product) {
        if (product.getId() == null) {
            return Response.success(integrationProductService.saveProduct(product) > 0);
        } else {
            return Response.success(integrationProductService.updateProduct(product) == 1);
        }

    }

    /**
     * 分页查询
     * @param product pro
     * @return
     */
    @PostMapping("/list_page")
    public Response<PageData<IntegrationProductRes>> selectList(IntegrationProductReq product) {
        return Response.success(integrationProductService.selectList(product));
    }


    /**
     * 删除商品
     * @param id id
     * @return
     */
    @PostMapping("/delete")
    public Response<Boolean> deleteProduct(Long id) {
        int res = integrationProductService.deleteProduct(id);
        if (res <= 0) {
            return Response.success(false, "该商品存在兑换记录,不允许删除");
        }
        return Response.success(true,"删除成功");
    }

    /**
     * 查询详情
     * @param id  id
     * @return
     */
    @PostMapping("/byId")
    public Response<IntegrationProductRes>  selectById(Long id) {
        return Response.success(integrationProductService.selectById(id));
    }
}
